<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">新增借阅</div>
    <el-form  :model="form" style="width: 800px" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="图书标准码" prop="bookNo">
        <el-select v-model="form.bookNo" clearable filterable placeholder="请选择" @change="selBook">
          <el-option
              v-for="item in books"
              :key="item.id"
              :label="item.bookNo"
              :value="item.bookNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图书名称" prop="bookName">
        <el-input v-model="form.bookName" disabled></el-input>
      </el-form-item>
      <el-form-item label="所需积分" prop="score">
        <el-input v-model="form.score" disabled></el-input>
      </el-form-item>
      <el-form-item label="图书数量" prop="bookNums">
        <el-input v-model="form.bookNums" disabled></el-input>
      </el-form-item>
      <el-form-item label="会员码" prop="userNo">
        <el-select v-model="form.userNo" filterable placeholder="请选择" @change="selUser">
          <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.username"
              :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input disabled v-model="form.userName" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户联系方式" prop="userPhone">
        <el-input disabled v-model="form.userPhone" disabled ></el-input>
      </el-form-item>
      <el-form-item label="用户积分" prop="userAccount">
        <el-input disabled v-model="form.userAccount" disabled ></el-input>
      </el-form-item>
      <el-form-item label="借出天数" prop="days">
        <el-input-number v-model="form.days"  label="借出天数"></el-input-number>
      </el-form-item>
      <div style="text-align: center; margin-top: 30px">
        <el-button type="primary" @click="save" size="medium">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "AddBorrow",
  data(){
    return{
      users:[],
      books:[],
      rules:{
        bookNo: [
          { required: true, message: '请输入图书标准码', trigger: 'blur'}
        ],
        userNo: [
          { required: true, message: '请输入会员码', trigger: 'blur'}
        ]
      },
      form:{
        days:1
      }
    }
  },
  created() {
    this.initUsers()
    this.initBooks()
  },
  methods:{
    selUser(){
      var user=this.users.find(v=>v.username==this.form.userNo)
      request.get("/user/"+user.id).then(res=>{
        if(res.code=='200'){
          this.$set(this.form,'userName',res.data.name)
          this.form.userName=res.data.name;
          this.form.userPhone=res.data.phone;
          this.form.userAccount=res.data.account;
        }
      })
    },
    selBook(){
      var book=this.books.find(v=>v.bookNo==this.form.bookNo)
      request.get('/book/'+book.id).then(res=>{
        if(res.code=='200'){
          this.$set(this.form,'bookName',res.data.name)
          this.form.bookName=res.data.name;
          this.form.score=res.data.score;
          this.form.bookNums=res.data.nums;
        }
      })
    },
    initUsers(){
      request.get('/user/list').then(res=>{
        if(res.code=='200'){
          this.users=res.data.filter(v=>v.status==true)
        }
      })
    },
    initBooks(){
      request.get('/book/list').then(res=>{
        if(res.code=='200'){
          this.books=res.data
        }
      })
    },
    save(){
      this.$refs['ruleForm'].validate((valid) => {
        if(valid){
          request.post("/borrow/save",this.form).then(res=>{
            if(res.code=='200'){
              this.$notify.success("添加成功");
              this.$refs['ruleForm'].resetFields()
            }else{
              this.$notify.error(res.msg)
            }
          })
        }
      })

    }
  }
}
</script>

<style scoped>

</style>